/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package serverside;

import java.io.*;
import java.net.*;
import com.cse.p2p.common.*;

import com.cse.p2p.database.*;
import com.cse.p2p.hash.*;
import com.cse.p2p.localfile.*;
import com.cse.p2p.parser.*;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import server.dicfile.DicFileGenerating;
import server.meta.UploadMeta;
import server.network.*;

/**
 *
 * @author TuyetNgan
 * Waiting thread : waiting for data send
 */
public class DataServerSide extends Thread {

	private ControlServerSide controlThread = null;
	private ConcurrentHashMap<String, UploadMeta> connectionList = new ConcurrentHashMap<String, UploadMeta>();
	private static Integer portControl = CommonSettings.SERVER_DATA_SERVERSOCKET;

	public void run() {

		try {
			ServerSocket serverSocket = null;
			boolean listening = true;
			int count = 0;
			try {
				serverSocket = new ServerSocket( portControl );
			}
			catch ( IOException e ) {
				System.err.println( "Could not listen on port: " + portControl );
				System.exit( -1 );
			}
			System.out.println( "Server start listening or data chanel on port " + portControl );
			while ( listening ) {
				new ServerReceiveDataTask( serverSocket.accept(), connectionList ).start();
				count++;
			}
			serverSocket.close();
		}
		catch ( IOException e ) {
		}

	}

	public synchronized void registerNewUpload( String remoteIP, UploadMeta data ) {
		this.connectionList.put( remoteIP, data );
	}

	public void setControlThread( ControlServerSide thread ) {
		this.controlThread = thread;
	}
}
